<?php
/*
 *
 * @(#) $Id: form_list_select.php,v 1.5 2009/11/11 08:13:11 mlemos Exp $
 *
 */

class form_list_select_class extends form_custom_class
{
	var $server_validate=0;
	var $options = array();
	var $name = '';

	var $STYLE = 'border-style: solid; border-color: #808080 #ffffff #ffffff #808080; border-width: 1px';
	var $CLASS = '';
	var $VALUE = '';
	var $SIZE;
	var $columns = array(
		array(
			'Type'=>'Input',
		),
		array(
			'Type'=>'Option',
		),
	);

	Function AddInput(&$form, $arguments)
	{
		if(!IsSet($arguments['OPTIONS'])
		|| GetType($arguments['OPTIONS'])!='array'
		|| count($this->options = $arguments['OPTIONS']) == 0)
			return('it were not specified the list select options');
		if(!IsSet($arguments['VALUE'])
		|| !IsSet($this->options[$this->VALUE = $arguments['VALUE']]))
			return('it was not specified a valid list select value');
		if(IsSet($arguments['Columns'])
		&& (GetType($arguments['Columns'])!='array'
		|| count($this->columns = $arguments['Columns']) == 0))
			return('it was not specified a valid list select columns');
		if(IsSet($arguments['Rows'])
		&& (GetType($arguments['Rows'])!='array'
		|| count($this->rows = $arguments['Rows']) == 0))
			return('it was not specified a valid list select columns');
		if(IsSet($arguments['CLASS']))
			$this->CLASS = $arguments['CLASS'];
		if(IsSet($arguments['STYLE']))
			$this->STYLE = $arguments['STYLE'];
		if(IsSet($arguments['SIZE']))
			$this->SIZE = $arguments['SIZE'];
		$this->name = $this->GenerateInputID($form, $this->input, 'radio');
		$to = count($this->options);
		for($o = 0, Reset($this->options); $o < $to; Next($this->options), ++$o)
		{
			$option = Key($this->options);
			$a = array(
				'TYPE'=>'radio',
				'NAME'=>$this->name,
				'ID'=>$this->name.$option,
				'VALUE'=>$option,
			);
			if($o == 0)
			{
				if(IsSet($arguments['LABEL']))
					$a['LABEL'] = $arguments['LABEL'];
				if(IsSet($arguments['ACCESSKEY']))
					$a['ACCESSKEY'] = $arguments['ACCESSKEY'];
			}
			if(!strcmp($option, $this->VALUE))
				$a['CHECKED'] = 1;
			if(strlen($error = $form->AddInput($a)))
				return($error);
		}
		Reset($this->options);
		$this->focus_input = $this->name.Key($this->options);
		return('');
	}

	Function AddInputPart(&$form)
	{
		$co = $this->columns;
		$tc = count($co);
		for($header = '<tr>', $i = $h = $c = 0; $c < $tc; ++$c)
		{
			if(!IsSet($co[$c]['Type']))
				return('list select column '.$c.' does not have a type');
			if($co[$c]['Type'] == 'Input')
				++$i;
			if(IsSet($co[$c]['Header']))
			{
				$header .= '<th>'.$co[$c]['Header'].'</th>';
				++$h;
			}
			else
				$header .= '<td>&nbsp;</td>';
		}
		if($i == 0)
			return('it was not specified any column with type Input');
		if($i > 1)
			return('it was specified more than one column with type Input');
		$to = count($this->options);
		if(strlen($error = $form->AddDataPart('<div style="'.(IsSet($this->SIZE) ? 'height: '.$this->SIZE.'em; ' : '').'overflow: auto;'.HtmlSpecialChars($this->STYLE).'"'.(strlen($this->CLASS) ? ' CLASS="'.HtmlSpecialChars($this->CLASS).'"' : '').'><table>')))
			return($error);
		if($h
		&& strlen($error = $form->AddDataPart($header.'</tr>')))
			return($error);
		for($o = 0, Reset($this->options); $o < $to; Next($this->options), ++$o)
		{
			if(strlen($error = $form->AddDataPart('<tr>')))
				return($error);
			$option = Key($this->options);
			for($c = 0; $c < $tc; ++$c)
			{
				if(strlen($error = $form->AddDataPart('<td>')))
					return($error);
				switch($co[$c]['Type'])
				{
					case 'Input':
						if(strlen($error = $form->AddInputPart($this->name.$option)))
							return($error);
						break;
					case 'Option':
						if(strlen($error = $form->AddDataPart(HtmlSpecialChars($this->options[$option]))))
							return($error);
						break;
					case 'Data':
						if(IsSet($co[$c]['Row'])
						&& IsSet($this->rows[$option][$row = $co[$c]['Row']])
						&& strlen($error = $form->AddDataPart($this->rows[$option][$row])))
							return($error);
						break;
				}
				if(strlen($error = $form->AddDataPart('</td>')))
					return($error);
			}
			if(strlen($error = $form->AddDataPart('</tr>')))
				return($error);
		}
		if(strlen($error = $form->AddDataPart('</table></div>')))
			return($error);
		return('');
	}

	Function GetInputValue(&$form)
	{
		return($form->GetCheckedRadioValue($this->name));
	}
	
	Function Connect(&$form, $to, $event, $action, &$context)
	{
		switch($event)
		{
			case 'ONCHANGE':
				$total = count($this->options);
				for($o = 0, Reset($this->options); $o < $total; Next($this->options), ++$o)
				{
					$option = Key($this->options);
					if(strlen($error = $form->Connect($this->name.$option, $to, $event, $action, $context)))
						return($error);
				}
				return('');
		}
		return($this->DefaultConnect($form, $to, $event, $action, $context));
	}


};

?>